const mongoConnected = require("../lib/mongoose_connect");
const mongoModels = require("../lib/mongoose_schemas");

mongoConnected.then(async _ => {
  let o_matches = await mongoModels.Match.find();
  debugger
  for (let match of o_matches) {
    let guijiCaozuo = await getCharacters(match.match_id);
  }
  console.log("all done");

})


/**
 * 
 * @param {String} match_id 
 */
function getCharacters(match_id) {
  return new Promise(async resolve => {
    let ofind = await mongoModels.Event.find({
      "match_id": match_id,
      $or: [{ "character": { "$ne": null } },
      { "attacker": { "$ne": null } },
      { "victim": { "$ne": null } },
      { "killer": { "$ne": null } },
      { "assistant": { "$ne": null } },
      { "reviver": { "$ne": null } },
      { "characters.0": { $exists: true } },
      { "survivors.0": { $exists: true } },
      { "drivers.0": { $exists: true } },
      { "fellowPassengers.0": { $exists: true } },
      ]
    });
    let LogMatchStart = ofind.find(e => e._T == "LogMatchStart");
    let eventsAfterMatchStart = ofind.filter(e => e._D.valueOf() - LogMatchStart._D.valueOf() >= 0);
    /**@type {Object.<string,Array<{time:Date,character:Object,in_event:String,info:string}>>} */
    let records = {};
    let add = (char, date, _T, info = "") => {
      let account_id = char.accountId;
      if (!records[account_id]) {
        records[account_id] = []
      }
      records[account_id].push({
        time: date,
        character: char,
        in_event: _T,
        info: info
      })
    }
    for (let event of eventsAfterMatchStart) {
      if (event._doc.characters) {
        // debugger
        for (let char of event._doc.characters) {
          add(char, event._doc._D, event._doc._T, "")
        }
      }
      if (event._doc.survivors) {
        for (let char of event._doc.survivors) {
          add(char, event._doc._D, event._doc._T, 'survivors')
        }
      }
      if (event._doc.drivers) {
        for (let char of event._doc.drivers) {
          add(char, event._doc._D, event._doc._T, 'redzone-end driver')
        }
      }
      if (event._doc.fellowPassengers) {
        for (let char of event._doc.fellowPassengers) {
          add(char, event._doc._D, event._doc._T, 'fellowPassenger')
        }
      }
      if (event._doc.character) {
        add(event._doc.character, event._doc._D, event._doc._T, "")
      }
      if (event._doc.attacker) {
        add(event._doc.attacker, event._doc._D, event._doc._T, "attacker")
      }
      if (event._doc.killer) {
        add(event._doc.killer, event._doc._D, event._doc._T, "killer")
      }
      if (event._doc.victim) {
        add(event._doc.victim, event._doc._D, event._doc._T, "victim")
      }
      if (event._doc.assistant) {
        add(event._doc.assistant, event._doc._D, event._doc._T, "assistant")
      }
      if (event._doc.reviver) {
        add(event._doc.reviver, event._doc._D, event._doc._T, "reviver")
      }

    }
    for (let ai in records) {
      let beforeSort = records[ai];
      let sorted = beforeSort.sort((a, b) => {
        return a.time.valueOf() - b.time.valueOf()
      });
      records[ai] = sorted;
    }

    let toSave = []
    for (let ai in records) {
      toSave.push({
        account_id: ai,
        match_id: match_id,
        arrayOfCharacters: records[ai]
      })
    }
    await mongoModels.PlayerCharacters.insertMany(toSave.map(e =>
      new mongoModels.PlayerCharacters(e)));
    console.log(match_id, "done!");
    resolve();

  })
}
